package com.xiaoxiao.regimen.provider;

import android.content.ContentValues;
import android.content.Entity;
import android.content.EntityIterator;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.BaseColumns;

import com.xiaoxiao.regimen.provider.RegimenContract.GroupsColumns;
import com.xiaoxiao.regimen.provider.RegimenContract.SyncColumns;

/**
 * Constants for the groups table.
 * 
 * @author jaroad
 *
 */
public final class Groups implements BaseColumns, GroupsColumns, SyncColumns {
    /**
     * This utility class cannot be instantiated
     */
    private Groups() {
    }

    /**
     * The content:// style URI for this table
     */
    public static final Uri CONTENT_URI = Uri.withAppendedPath(RegimenContract.AUTHORITY_URI, "groups");

    /**
     * The content:// style URI for this table joined with details data from
     * {@link Data}.
     */
    public static final Uri CONTENT_SUMMARY_URI = Uri.withAppendedPath(RegimenContract.AUTHORITY_URI,
            "groups_summary");

    /**
     * The MIME type of a directory of groups.
     */
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/group";

    /**
     * The MIME type of a single group.
     */
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/group";
    
    public static EntityIterator newEntityIterator(Cursor cursor) {
        return new EntityIteratorImpl(cursor);
    }

    private static class EntityIteratorImpl extends CursorEntityIterator {
        public EntityIteratorImpl(Cursor cursor) {
            super(cursor);
        }

        @Override
        public Entity getEntityAndIncrementCursor(Cursor cursor) throws RemoteException {
            // we expect the cursor is already at the row we need to read from
            final ContentValues values = new ContentValues();
            DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, values, _ID);
            DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, values, DIRTY);
            DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, values, VERSION);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, TITLE);
            DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, values, GROUP_VISIBLE);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, SYNC1);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, SYNC2);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, SYNC3);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, SYNC4);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, SYNC5);
            DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, values, DELETED);
            DatabaseUtils.cursorLongToContentValuesIfPresent(cursor, values, GROUP_IS_READ_ONLY);
            DatabaseUtils.cursorStringToContentValuesIfPresent(cursor, values, NOTES);
            cursor.moveToNext();
            return new Entity(values);
        }
    }
}
